package sctu.edu.java.year2016.team07.project.client;

import java.io.*;
import java.util.Date;

/**
 * Created by Huang Suixin on 2016/6/15.
 */
public class User {
    private String name;
    private String Tel;
    private String Add;

    protected void setName(String name) {
        this.name = name;
    }

    protected String getName() {
        return name;
    }

    protected void setAdd(String Add) {
        this.Add = Add;
    }

    protected String getAdd() {
        return Add;
    }

    protected void setTel(String Tel) {
        this.Tel = Tel;
    }

    protected String getTel() {
        return Tel;
    }

    //查找方法，查找成功返回该问题的解决方法，查找失败空字符串
    protected String  search(String str) throws FileNotFoundException {

        File dir = new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\question");
        String[] filelist = dir.list();//文件夹中文件的名字(含后缀.txt)

        String[] filename = new String[filelist.length];//文件的名字（不含后缀）
        for (int i = 0; i  < filelist.length; i++) {
            filename[i] = filelist[i].replace(".txt","");
        }

        String foundname = "";
        for (int i = 0; i < filename.length; i++) {
            if (str.contains(filename[i])) {
                foundname = filename[i];
                break;
            }
        }

        char[] getStr = new char[4800];     //用来存放从文件读取的内容；
        if (foundname.equals("")) {
            return "";
        } else {
            File file = new File(dir , foundname);
            FileReader fr = new FileReader(file);
            try {
                fr.read(getStr);        //将文件中的内容读出来，存到getStr字符数组中
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new String(getStr);
    }

    //反馈方法
    protected void record(String question){
        File dir = new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\record");
        File file = new File(dir,name);//以用户名命名该用户反馈的问题文件

        //得到今天的时间
        Date today = new Date();
        String date = String.format("%tF",today) + " " +  String.format("%tT",today);

        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
            //若文件存在，说明该用户反馈过但还未处理，直接在该文件后面添加新问题。否则，创建文件，写入文件信息。
            if(!file.exists()){
                bw.write(date + " ");
                bw.write("反馈的问题：" + question + "\n");
            }else{
                file.createNewFile();
                bw.write("姓名：" + name + " \n");
                bw.write("电话：" + Tel + " \n");
                bw.write("地址：" + Add + " \n");
                bw.write(date + " ");
                bw.write("反馈的问题：" + question + "\n");
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}